package com.niodata.dt.fs.us3;

import cn.ucloud.ufile.api.object.ObjectApiBuilder;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;

public class Us3Uploader {

  private final ObjectApiBuilder apiBuilder;
  private final String bucket;
  private final String key;

  public Us3Uploader(String bucket, String key, ObjectApiBuilder apiBuilder) {
    this.apiBuilder = apiBuilder;
    this.bucket = bucket;
    this.key = key;
  }

  public void put(byte[] content) throws IOException {
    try {
      apiBuilder.putObject(new ByteArrayInputStream(content),
            content.length, "application/octet-stream").nameAs(key).toBucket(bucket).execute();
    } catch (Exception ex) {
      throw new IOException(ex);
    }
  }

  public void put(File file) throws IOException {
    try {
      apiBuilder.putObject(file, "application/octet-stream")
            .nameAs(key)
            .toBucket(bucket).execute();
    } catch (Exception ex) {
      throw new IOException(ex);
    }
  }
}
